//
//  RootViewController.m
//  IMHuanXin
//
//  Created by 小獾科技 on 2017/7/26.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "RootViewController.h"
#import "HomeViewController.h"
#import "DiaryViewController.h"
#import "ExperienceViewController.h"
#import "MineViewController.h"
#import "LoginViewController.h"
@interface RootViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) NSMutableArray *vcList;
@property (nonatomic, copy) CompleteVoidBlock loginSuccessBlock;// 登录成功后调用
@property (nonatomic, assign) BOOL isShowLoginVC;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.vcList = [NSMutableArray arrayWithCapacity:0];
    [self setTabBarVC];
    self.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLoginVCNoti:) name:kNotiShowLoginVC object:nil];
}

#pragma mark - keyboard noti
- (void)showLoginVCNoti:(NSNotification *)noti {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];

    _loginSuccessBlock = (CompleteVoidBlock)noti.object;
    [self performSelector:@selector(goToLoginVC) withObject:nil afterDelay:0.2];
}
- (void)goToLoginVC {
    
    if (_isShowLoginVC) {
        return;
    }
    _isShowLoginVC = YES;

    CompleteVoidBlock closeBlock = ^ {
        
        //        NSLog(@"登录页面dimiss");
        self.isShowLoginVC = NO;
    };
    
    LoginViewController *loginVC = [LoginViewController new];
    loginVC.loginSuccessBlock = _loginSuccessBlock;
    loginVC.closeLoginVCBlock = closeBlock;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    if (viewController.view.tag == 103 && !IS_LOGIN) {
        
        // 登录
        @weakobj(self);
        CompleteVoidBlock loginSuccessBlock = ^(void) {
            
            @strongobj(self);
            [self setSelectedIndex:3];
        };
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotiShowLoginVC object:loginSuccessBlock];
        
        return NO;
    }
    if(viewController.view.tag == 100)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    }
    return YES;
}

- (void)setTabBarVC
{
    [self addNavi:[HomeViewController new] title:@"首页" imageName:@"bar_home" andTag:100];
    [self addNavi:[DiaryViewController new] title:@"日记" imageName:@"bar_diary" andTag:101];
    [self addNavi:[ExperienceViewController new] title:@"体验" imageName:@"bar_experience" andTag:102];
    [self addNavi:[MineViewController new] title:@"我的" imageName:@"bar_mine" andTag:103];
}

- (void)addNavi:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName andTag:(NSInteger)tag
{
    vc.view.tag = tag;
    [self.vcList addObject:vc];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
    UITabBarItem *barItem = [[UITabBarItem alloc] initWithTitle:title
                                                          image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                  selectedImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [barItem setImageInsets:UIEdgeInsetsMake(-2, 0, 2, 0)];
    [barItem setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    navi.tabBarItem = barItem;
    barItem.tag = tag;
    navi.view.tag = tag;
    [self addChildViewController:navi];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
